###############################################################################
# SuperLinks                                                                  #
# Copyright (c) 2000-2007 Greg Nolle (http://greg.nolle.co.uk)                #
###############################################################################
# This program is free software; you can redistribute it and/or modify it     #
# under the terms of the GNU General Public License as published by the Free  #
# Software Foundation; either version 2 of the License, or (at your option)   #
# any later version.                                                          #
#                                                                             #
# This program is distributed in the hope that it will be useful, but WITHOUT #
# ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or       #
# FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License for   #
# more details.                                                               ##                                                                             #
# You should have received a copy of the GNU General Public License along     #
# with this program; if not, write to the Free Software Foundation, Inc.,     #
# 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.                 #
###############################################################################
# ControlPanel/ModifyLevel.pm.pl -> ModifyLevel module                        #
###############################################################################
# DON'T EDIT BELOW UNLESS YOU KNOW WHAT YOU'RE DOING!                         #
###############################################################################
package CP::ModifyLevel;

BEGIN { require "System.pm.pl"; import System qw($SYSTEM); }
BEGIN { require "General.pm.pl"; import General qw($GENERAL); }
require "Error.pm.pl";
require "Standard.pm.pl";
require "Authenticate.pm.pl";
require "HashToString.pm.pl";

use strict;

require "ControlPanel/Output/ModifyLevel.pm.pl";

sub new {
  my ($class) = @_;
  my $self    = {};
  return bless($self, $class);
}

sub DESTROY { }

###############################################################################
# show subroutine
sub show {
  my $self = shift;
  my %in = (DB => undef, ERROR => "", @_);

  #----------------------------------------------------------------------#
  # Authenticating...                                                    #

  &Authenticate::CP(DB => $in{'DB'}, ACTION => "LEVELS_MODIFY");

  #----------------------------------------------------------------------#
  # Checking fields...                                                   #

  if (!$SL::QUERY{'LID'}) {
    &Error::Error("CP", MESSAGE => "You didn't specify a Level ID (LID)");
  }
  
  my $Level = $in{'DB'}->BinarySelect(
    TABLE => "Levels",
    KEY   => $SL::QUERY{'LID'}
  );
  unless ($Level) {
    &Error::Error("CP", MESSAGE => "The Level ID (LID) you specified is invalid");
  }

  #----------------------------------------------------------------------#
  # Getting data...                                                      #

  $Level->{'permissions'} = eval($Level->{'PERMISSIONS'});

  my ($Categories, $CategoriesIndex) = $in{'DB'}->Query(
    TABLE   => "Categories",
    SORT    => "FULL_TITLE",
    BY      => "A-Z"
  );
  
  my %INPUT;

  $INPUT{'LEVEL'}         = $Level;  
  $INPUT{'CATEGORIES'}    = $Categories;
  $INPUT{'CATEGORIES_IX'} = $CategoriesIndex;
  
  #----------------------------------------------------------------------#
  # Printing page...                                                     #

  my $Skin = Skin::CP::ModifyLevel->new();

  &Standard::PrintHTMLHeader();
  print $Skin->show(input => \%INPUT, error => $in{'ERROR'});
  
  return 1;
}

###############################################################################
# do subroutine
sub do {
  my $self = shift;
  my %in = (DB => undef, @_);

  #----------------------------------------------------------------------#
  # Authenticating...                                                    #

  &Authenticate::CP(DB => $in{'DB'}, ACTION => "LEVELS_MODIFY");

  #----------------------------------------------------------------------#
  # Checking fields...                                                   #

  if (!$SL::QUERY{'LID'}) {
    &Error::Error("CP", MESSAGE => "You didn't specify a Level ID (LID)");
  }
  
  my $Level = $in{'DB'}->BinarySelect(
    TABLE => "Levels",
    KEY   => $SL::QUERY{'LID'}
  );
  unless ($Level) {
    &Error::Error("CP", MESSAGE => "The Level ID (LID) you specified is invalid");
  }

  my @Fields = (
    { name => "TITLE"                   , required => 1, size => 256 },
    
    { name => "SUPERUSER"               , required => 0              },

    { name => "ADMINS_VIEW"             , required => 0              },
    { name => "ADMINS_CREATE"           , required => 0              },
    { name => "ADMINS_MODIFY"           , required => 0              },
    { name => "ADMINS_REMOVE"           , required => 0              },
    { name => "BACKUPS_VIEW"            , required => 0              },
    { name => "BACKUPS_CREATE"          , required => 0              },
    { name => "BACKUPS_REMOVE"          , required => 0              },
    { name => "BACKUPS_RESTORE"         , required => 0              },
    { name => "CATEGORIES_VIEW"         , required => 0              },
    { name => "CATEGORIES_CREATE"       , required => 0              },
    { name => "CATEGORIES_MODIFY"       , required => 0              },
    { name => "CATEGORIES_REMOVE"       , required => 0              },
    { name => "LANGUAGESETS_VIEW"       , required => 0              },
    { name => "LANGUAGESETS_CREATE"     , required => 0              },
    { name => "LANGUAGESETS_MODIFY"     , required => 0              },
    { name => "LANGUAGESETS_REMOVE"     , required => 0              },
    { name => "RESOURCES_VIEW"          , required => 0              },
    { name => "RESOURCES_APPROVE"       , required => 0              },
    { name => "RESOURCES_CREATE"        , required => 0              },
    { name => "RESOURCES_MODIFY"        , required => 0              },
    { name => "RESOURCES_REMOVE"        , required => 0              },
    { name => "SKINS_VIEW"              , required => 0              },
    { name => "SKINS_CREATE"            , required => 0              },
    { name => "SKINS_MODIFY"            , required => 0              },
    { name => "SKINS_REMOVE"            , required => 0              },
    { name => "STYLESHEETS_VIEW"        , required => 0              },
    { name => "STYLESHEETS_CREATE"      , required => 0              },
    { name => "STYLESHEETS_MODIFY"      , required => 0              },
    { name => "STYLESHEETS_REMOVE"      , required => 0              },
    { name => "TEMPLATESETS_VIEW"       , required => 0              },
    { name => "TEMPLATESETS_CREATE"     , required => 0              },
    { name => "TEMPLATESETS_MODIFY"     , required => 0              },
    { name => "TEMPLATESETS_REMOVE"     , required => 0              },
    { name => "EMAIL"                   , required => 0              },
    { name => "EXPIRE"                  , required => 0              },
    { name => "UPDATE"                  , required => 0              },
    { name => "GENERALOPTIONS"          , required => 0              },
    { name => "SYSTEMOPTIONS"           , required => 0              },
    { name => "IMPORTEXPORT"            , required => 0              },
    { name => "INSTALLPACKAGE"          , required => 0              },
    { name => "REVIEWS_VIEW"            , required => 0              },
    { name => "REVIEWS_MODIFY"          , required => 0              },
    { name => "REVIEWS_REMOVE"          , required => 0              },
    { name => "LEVELS_VIEW"             , required => 0              },
    { name => "LEVELS_CREATE"           , required => 0              },
    { name => "LEVELS_MODIFY"           , required => 0              },
    { name => "LEVELS_REMOVE"           , required => 0              },
    { name => "EXTRAFIELDS_VIEW"        , required => 0              },
    { name => "EXTRAFIELDS_CREATE"      , required => 0              },
    { name => "EXTRAFIELDS_MODIFY"      , required => 0              },
    { name => "EXTRAFIELDS_REMOVE"      , required => 0              }
  );

  my (%RECORD, %INPUT);

  my @Error;
  foreach my $field (@Fields) {
    if ($field->{'required'} && $SL::QUERY{'FORM_'.$field->{'name'}} eq "") {
      push(@Error, "MISSING-".$field->{'name'});
    } elsif ($field->{'size'} && $SL::QUERY{'FORM_'.$field->{'name'}} ne "" && length($SL::QUERY{'FORM_'.$field->{'name'}}) > $field->{'size'}) {
      push(@Error, "TOOLONG-".$field->{'name'});
    } else {
      $RECORD{ $field->{'name'} } = $SL::QUERY{'FORM_'.$field->{'name'}};
    }
  }

  foreach my $key (grep(/^FORM_CATEGORY_(.*?)_(.*)$/, keys %SL::QUERY)) {
    $key =~ m/^FORM_CATEGORY_(.*?)_(.*)$/;
    $RECORD{'CATEGORIES'}->{$1}->{$2} = $SL::QUERY{$key};
  }

  if (scalar(@Error) >= 1) {
    $self->show(DB => $in{'DB'}, ERROR => \@Error);
    return 1;
  }

  #----------------------------------------------------------------------#
  # Updating data...                                                     #

  $RECORD{'TITLE'} = &Standard::HTMLize($RECORD{'TITLE'});

  my $HashToString = HashToString->new();

  my %Permissions = %RECORD;
  delete($Permissions{'TITLE'});

  (
    $in{'DB'}->Update(
      TABLE   => "Levels",
      VALUES  => {
        TITLE       => $RECORD{'TITLE'},
        PERMISSIONS => $HashToString->Write(\%Permissions)
      },
      KEY     => $SL::QUERY{'LID'}
    )
  ) || &Error::Error("CP", MESSAGE => "Error updating record. $in{'DB'}->{'ERROR'}");

  $RECORD{'ID'} = $SL::QUERY{'LID'};

  $INPUT{'RECORD'} = \%RECORD;
  $INPUT{'LEVEL'}  = $Level;

  #----------------------------------------------------------------------#
  # Printing page...                                                     #

  my $Skin = Skin::CP::ModifyLevel->new();

  &Standard::PrintHTMLHeader();
  print $Skin->do(input => \%INPUT);
  
  return 1;
}

1;